export default {
  watch: {
    projectId: function (newProjectId) {
      this.getEnvPage({pageNum: 1, pageSize: 10, projectId: this.$store.getters.getProjectId});
    }
  },
  computed: {
    projectId: function () {
      return this.$store.getters.getProjectId;
    },
  },
  created() {
    this.getEnvPage({pageNum: 1, pageSize: 10, projectId: this.$store.getters.getProjectId});
  },
  data() {
    return {
      id: 0,
      modalAction: "",
      input: "",
      ta: 50,
      envList: {
        data: {
          list: []
        }

      },
      dialogFormVisible: false,
      testName: "",
      remark: "",
      projectContent: "",
      title: "添加项目",
      formLabelWidth: "100px",
    };
  },
  methods: {
    createTimeFormatter(row, column) {
      return this.$dateFomat.timestampToTime(row.createTime * 1000);
    },
    updateTimeFormatter(row, column) {
      return this.$dateFomat.timestampToTime(row.updateTime * 1000);
    },
    getEnvPage(p) {
      this.$env.page(p).then((res) => {
        if (res.code == 200) {
          this.envList = res;
          console.log(res);
        }
      }).catch((res) => {
        console.log(res);
      });
    },
    tjxm() {
      this.dialogFormVisible = true
      this.testName = "";
      this.remark = "";
      this.modalAction = "add";
      this.title = "添加环境";
    },
    cz() {
      this.input = "";
    },
    dj() {
      this.dialogFormVisible = false
      if (this.modalAction === 'add') {
        this.$env
          .envAdd({
            name: this.testName,
            remark: this.remark,
            projectId: this.$store.getters.getProjectId
          })
          .then((res) => {
            if (res.code == 200) {
              this.$message.success(res.message);
              this.getEnvPage({
                pageNum: this.envList.data.pageNum,
                pageSize: this.envList.data.pageSize,
                projectId: this.$store.getters.getProjectId
              });
            }
          })
          .catch((res) => {
            this.$message.error("系统异常");
          });
      } else if (this.modalAction === 'edit') {
        this.$env.envUpdate({
          id: this.id,
          name: this.testName,
          remark: this.remark,
          projectId: this.$store.getters.getProjectId
        }).then((res) => {
          if (res.code == 200) {
            this.$message.success(res.message);
            this.getEnvPage({
              pageNum: this.envList.data.pageNum,
              pageSize: this.envList.data.pageSize,
              projectId: this.$store.getters.getProjectId
            });
          }
        })
          .catch((res) => {
            console.log(res);
            this.$message.error("系统异常");
          });
      }
    },
    handleEdit(index, value) {
      this.testName = value.name;
      this.remark = value.remark;
      this.dialogFormVisible = true;
      this.modalAction = "edit";
      this.title = "修改环境";
      this.id = this.envList.data.list[index].id
      console.log(index, value);
    },
    handleSizeChange(val) {
      this.getEnvPage({
        pageNum: this.envList.data.pageNum,
        pageSize: val,
        projectId: this.$store.getters.getProjectId
      });
    },
    handleCurrentChange(val) {
      this.getEnvPage({
        pageNum: val,
        pageSize: this.envList.data.pageSize,
        projectId: this.$store.getters.getProjectId
      });
    },
    handleDelete(index) {
      let id = this.envList.data.list[index].id;
      this.$env
        .envDelete({id: id})
        .then((res) => {
          console.log(res);
          this.getEnvPage({
            pageNum: this.envList.data.pageNum,
            pageSize: this.envList.data.pageSize,
            projectId: this.$store.getters.getProjectId
          });
        })
        .catch((res) => {
          console.log(res);
        });
    },
  },
}
